/** 
  *@CopyRight(c)2009 company Co.Ltd. 
  *@All right reserved.
  *@module name: App.OnlineServices.OnlineChatRoom
  *@description:
  *
  *@author: lugj
  *@version: 1.0
  *@date: 2009/10/16
  *@last date: 2009/10/16
  *@history:
    1.
    2.
  *@bugs:
    1. 
    2. 
  *
*/ 

Ext.namespace("App.OnlineServices");

Ext.ux.IFrameComponent = Ext.extend(Ext.BoxComponent, {
		onRender : function(ct, position){
			this.el = ct.createChild({tag: 'iframe', id: 'framepanel' + this.id, frameBorder: 1, src: this.url});
	}
}); 

//OnlineChatRoom Class
//config: {id: , }
App.OnlineServices.OnlineChatRoom = function(config){
	//generating the global unique id.
	this.config = config;

	this.MODULE_ID = (typeof(this.config) == 'undefined') ? null : this.config.id;

	this.MODULE_ID = (this.MODULE_ID == null) ? App.Base.Common.func.getSystemDateTimeMark() : this.MODULE_ID;
	
	this.MODULE_ID = 'OLCHATROOM' + this.MODULE_ID;
	
	App.OnlineServices.OnlineChatRoom.MSG_RECEIVE_WINDOW = "receiveFrom" + this.MODULE_ID;
	App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW = "sendTo" + this.MODULE_ID;

	App.OnlineServices.OnlineChatRoom.RECEIVE_MSG_WINDOW_STATUS = App.Base.common.param.CHATROOM_RECEIVE_MSG_WINDOW_STATUS_OFF;

	App.OnlineServices.OnlineChatRoom.sendMsg = function () {
		if (App.OnlineServices.OnlineChatRoom.RECEIVE_MSG_WINDOW_STATUS == App.Base.common.param.CHATROOM_RECEIVE_MSG_WINDOW_STATUS_OFF)
		{
			alert('请选择‘进入聊天’');
			return;
		}

		if (Ext.getCmp(App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW).getValue().length == 0)
		{
			alert("发送的消息为空");
			return;
		}

		sendMsg(Ext.getCmp(App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW).getValue());
		Ext.getCmp(App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW).setValue("");
	};

	this.uxIFrameMsgRecv = new Ext.ux.IFrameComponent({
		id: App.OnlineServices.OnlineChatRoom.MSG_RECEIVE_WINDOW, 
		url: 'js/OnlineServices/ChatRoomMsgRecvBkgWindow.html'
	});

    this.formPanel = new Ext.FormPanel({
		id: 'fp' + this.MODULE_ID,
		autoDestroy: true,
		border: false,

		layout: 'border',
		items: [{
			region:'west',
			id: 'fpW' + this.MODULE_ID,
			width: 600,
			minWidth: 560,

			//frame: true,
			border: false,
			autoScroll: true,

			split : true,
			collapsible : true,
			bodyStyle:'padding:5px 5px 5px 1px',

			bbar: new Ext.Toolbar({
				id: 'bbar01' + this.MODULE_ID,
				border: false,

				items : ['->', '-',
					new Ext.Button({
						id: 'btnAdd' + this.MODULE_ID,
						text: '<font color="red">发送消息(CTRL+ENTER)</font>',
						iconCls: 'x-menu-onlineservices-chatroom-send',
						handler: function(){
							App.OnlineServices.OnlineChatRoom.sendMsg();
						},
						scope: this
					}), '-',
					new Ext.Button({
						id: 'clearUp' + this.MODULE_ID,
						text: '<font color="blue">清 空</font>',
						iconCls: 'x-menu-onlineservices-chatroom-clear',
						tooltip: '清空发送窗口中的信息',
						handler: function(){
							Ext.getCmp(App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW).setValue("");
							Ext.getCmp(App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW).focus();
						},
						scope: this
					})
				]
			}),

			layout: 'border',
			items: [{
				region:'north',
				id: 'fpWN' + this.MODULE_ID,
				title: '消息接收窗口:',
				height: 290,
				border: false,
				frame: true,
				autoScroll: true,
				split: true,
				collapsible : true,
				bodyStyle:'padding:5px 5px 5px 2px',

				bbar: new Ext.Toolbar({
					id: 'bbar02' + this.MODULE_ID,
					border: true,					 
					items : ['-', {
							iconCls: 'x-menu-onlineservices-chatroom-user',
							text: '<font color="red">我的昵称:</font>',
							tooltip: '必填：我在聊天室中显示的昵称'
						},
						new Ext.form.TextField({
							id: 'nick' + this.MODULE_ID,
							name: 'nick' + this.MODULE_ID,
							width: 120,
							//fieldLabel: '<font color="#FF0000">昵称:</font>',
							emptyText: "请填写我的昵称"
						}),
						'-',
						new Ext.Button({
							id: 'enterChat' + this.MODULE_ID,
							text: '<font color="blue">进入聊天</font>',
							iconCls: 'x-menu-onlineservices-chatroom-join',
							handler: function(){
								var nick = Ext.getCmp('nick' + this.MODULE_ID).getValue();
								if (nick == null || trim(nick).length == 0 )
								{
									alert("‘我的昵称’不能为空");
									return;
								}
								App.OnlineServices.OnlineChatRoom.RECEIVE_MSG_WINDOW_STATUS = App.Base.common.param.CHATROOM_RECEIVE_MSG_WINDOW_STATUS_ON;
								Ext.getCmp('nick' + this.MODULE_ID).disable();
								Ext.getCmp('enterChat' + this.MODULE_ID).disable();
								chat_init(nick);
							},
							scope: this
						}), '-'
					]
				}),

				layout: 'fit',
				items: [
					this.uxIFrameMsgRecv
				]
			}, {
				region:'center',
				id: 'fpWC' + this.MODULE_ID,
				frame: true,
				border: false,
				autoScroll: true,
				bodyStyle:'padding:5px 5px 5px 2px',
				layout: 'fit',
				items: [
					new Ext.form.HtmlEditor({
						id: App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW,
						name: App.OnlineServices.OnlineChatRoom.MSG_SEND_WINDOW,
						hideLabel: true,
						enableLinks: false,
						//anchor: '130%',
						//readOnly: true,
						plugins: [new Ext.ux.HtmlEditorImageInsertPlugin({
							popTitle: '插入图片',
							popMsg: '请输入图片的 URL...',
							popWidth: 400,
							popValue: 'http://'
						})],
						listeners: {
							initialize: function() {
								if (Ext.isIE) {
									Ext.EventManager.un(this.doc, 'keydown', this.fixKeys, this);
								}
								Ext.EventManager.on(this.doc, Ext.isGecko ? 'keypress' : 'keydown', function(e) {
									if ((e.getKey() == e.ENTER) && e.ctrlKey) {
										App.OnlineServices.OnlineChatRoom.sendMsg();
									}
								}, this);
								if (Ext.isIE) {
									Ext.EventManager.on(this.doc, 'keydown', this.fixKeys, this);
								}
							}
							//,scope: this
						}
					})
/*
					new Ext.form.TextArea({
						id: 'sendTo' + this.MODULE_ID,
						name: 'sendTo' + this.MODULE_ID,
						hideLabel: true,
						anchor: '100% -53',  // anchor width by percentage and height by raw adjustment
						//readOnly: true,
						autoCreate: {
							tag: 'textarea',
							//style: 'width:100px;height:60px;',
							autocomplete: 'on',
							wrap: 'hard',
							style:'overflow-y: scroll'
						}
					})
*/
				]
			}]
		}, {
			region:'center',
			id: 'fpC' + this.MODULE_ID,
			border: false,
			autoScroll: true,
			frame: true,
			layout: 'fit',
			items: [
			]
		}],
		
		listeners: {
			 destroy: function (p) {
				//MAYBE MEMs LEAK:
				//document.getElementsByTagName("IFRAME")[0].src = "javascript:false";
				this.uxIFrameMsgRecv.destroy();
				//
				if (App.OnlineServices.OnlineChatRoom.RECEIVE_MSG_WINDOW_STATUS == App.Base.common.param.CHATROOM_RECEIVE_MSG_WINDOW_STATUS_OFF)
				{
					return;
				}
				App.OnlineServices.OnlineChatRoom.RECEIVE_MSG_WINDOW_STATUS = App.Base.common.param.CHATROOM_RECEIVE_MSG_WINDOW_STATUS_OFF;
				leaveChat();
			},
			scope: this
		}
	});

	this.formPanel.doLayout();

	//alert( this.formPanel.getLayout();
	
	//defined methods.
    if((typeof App.OnlineServices.OnlineChatRoom.SIMULATE_SHARED_METHODS_FLAG) == "undefined")
    {
        App.OnlineServices.OnlineChatRoom.SIMULATE_SHARED_METHODS_FLAG = true;   

        App.OnlineServices.OnlineChatRoom.prototype.getMainPanel = function(){   
            return this.formPanel;  
        };
 
        //simulating the class's constructor().
        var __ctor = function(){
            //alert('called __constructor() ok.');
        }();

    }
}